<template>
	<basic-container>
		<avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" ref="crud" @row-del="rowDel" v-model="form" :permission="permissionList" @row-update="rowUpdate" @row-save="rowSave" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
			<template slot-scope="{ row }" slot="status">
				<el-tag type="danger" v-if="row.status == 1">正常</el-tag>
				<el-tag type="warning" v-else-if="row.status == 2">逾期</el-tag>
				<el-tag v-else-if="row.status == 3">撤销</el-tag>
				<el-tag v-else-if="row.status == 4">被撤销</el-tag>
				<el-tag v-else-if="row.status == 5">可延续</el-tag>
				<el-tag v-else-if="row.status == 6">注销</el-tag>
			</template>
			<template slot-scope="{ row }" slot="search">
				<avue-form :option="topSelection" v-model="selectform" @submit="handleSubmit" @reset-change="handleReset">
				</avue-form>
			</template>
			<template slot-scope="{ type, size, row, index }" slot="menu">
				<el-button type="text" :size="size" @click="openpassword(row)" v-if="permission.regist_reset">重置密码</el-button>
				<el-button type="text" :size="size" v-if="permission.regist_revoke" @click="handlerevork(row)">撤销</el-button>
				<el-button :size="size" type="text" @click="toshenpi(row.id, 'view')">查看</el-button>
				<!-- <el-button
          :size="size"
          type="text"
          v-if="row.status !== 3"
          @click="toshenpi(row.id, 'edit')"
          >编辑</el-button
        > -->
			</template>

			<template slot-scope="{ row }" slot="order">
				<div style="width: 300px;">
					<pdf :src="row.electronicSealDocumentUrl"></pdf>
				</div>
			</template>

			<template slot-scope="{ row }" slot="category">
				<el-tag>{{ row.categoryName }}</el-tag>
			</template>
		</avue-crud>
		<el-dialog title="撤销" :visible.sync="dialogrevoke" width="40%" append-to-body :before-close="handleClose">
			<span>确定要对用户进行证件撤销工作？撤销后该证件不可使用，且
				用户5年内不可再次注册</span>
			<div>
				<div style="margin: 20px 0">
					<span style="color: red">*</span> 撤销说明
				</div>

				<el-input type="textarea" placeholder="请输入内容" v-model="textarearevoke" maxlength="200" show-word-limit>
				</el-input>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogrevoke = false">取 消</el-button>
				<el-button type="primary" @click="handlerevoke">确 定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="重置密码" :visible.sync="dialogpassword" width="40%" append-to-body :before-close="handleClose">
			<span>重置密码，用户将不可用原密码继续登录</span>
			<div style="height: 20px"></div>
			<el-form :model="numberValidateForm" ref="numberValidateForm" label-width="80px" class="demo-ruleForm" :rules="rules">
				<el-form-item label="新密码" prop="password">
					<el-input type="password" v-model.number="numberValidateForm.password" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="password1">
					<el-input v-model.number="numberValidateForm.password1" autocomplete="off" type="password"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogpassword = false">取 消</el-button>
				<el-button type="primary" @click="changepass">确 定</el-button>
			</span>
		</el-dialog>
	</basic-container>
</template>
<script>
// import { remove, update, add } from '@/api/desk/notice';
import { mapGetters } from 'vuex';
// import { acseRegisterpage, updatePasswordNoOldPassword, acserevoke } from '@/api/pxxc/index';
import md5 from 'js-md5';
import pdf from 'vue-pdf';
export default {
	components: { pdf },
	data() {
		let validatePass2 = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请再次输入密码'));
			} else if (value !== this.numberValidateForm.password) {
				callback(new Error('两次输入密码不一致!'));
			} else {
				callback();
			}
		};
		return {
			selectform: {},

			dialogrevoke: false,
			dialogpassword: false,
			numberValidateForm: {
				age: '',
			},
			rules: {
				password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
				password1: [{ required: true, validator: validatePass2, trigger: 'blur' }],
			},
			textarearevoke: '',
			value: '',
			form: {},
			query: {},
			loading: true,
			page: {
				pageSize: 10,
				currentPage: 1,
				total: 0,
			},
			selectionList: [],
			option: {menuType:'button',
				height: 'auto',
				calcHeight: 30,
				dialogWidth: 950,
				tip: false,
				searchShow: true,
				searchMenuSpan: 6,
				border: true,
				selection: true,
				excelBtn: true,
				dialogClickModal: false,
				searchBtn: false,
				emptyBtn: false,
				addBtn: false,
				delBtn: false,
				editBtn: false,
				menu: false,
				column: [
					{
						label: '考试用户名',
						prop: 'name',
						span: 24,
						row: true,
					},
					{
						label: '用户寸照',
						listType: 'picture-img',
						type: 'upload',
						prop: 'documentsUrl',
						propsHttp: {
							res: 'data',
						},
						slot: true,
					},

					{
						label: '身份证号',
						prop: 'documentNum',
						span: 24,
						row: true,
					},
					{
						label: '证书',
						prop: 'order',
						span: 24,
						row: true,
					},
				],
			},
			data: [],
			passwordis: '',
			revokeId: '',
		};
	},
	computed: {
		topSelection() {
			return {
				submitText: '搜索',
				emptyText: '清空',
				column: [
					{
						label: '审核机构',
						span: 6,
						labelWidth: 120,
						prop: 'firstExamineDeptId',
						type: 'select',
						dicUrl: '/api/blade-system/dept/officialSelect',
						props: {
							label: 'deptName',
							value: 'id',
						},
					},
					{
						label: '专业类别',
						span: 6,
						labelWidth: 120,
						prop: 'acseSpecialityTypeId',
						type: 'select',
						dicUrl: '/api/blade-specialityType/specialityType/select',
						props: {
							label: 'name',
							value: 'id',
						},
						display: Boolean(this.permission.regist_category),
					},
					{
						label: '注册类型',
						prop: 'registerCategory',
						span: 6,
						labelWidth: 120,
						row: true,
						type: 'select',
						display: Boolean(this.permission.registerCategory),
						dicData: [
							{
								label: '初始注册',
								value: 'FIRST',
							},
							{
								label: '初始逾期注册',
								value: 'FIRST_OVERDUE',
							},
							{
								label: '延续注册',
								value: 'KEEP_ON',
							},
							{
								label: '变更注册',
								value: 'CHANGE',
							},
							{
								label: '重新注册',
								value: 'AGENT',
							},
							{
								label: '撤销注册',
								value: 'REVOKE',
							},
						],
					},
					{
						label: '聘用单位',
						prop: 'employer',
						span: 6,
						labelWidth: 120,
						display: Boolean(this.permission.regist_employer),
					},
					{
						label: '用户名/身份证',
						prop: 'param',
						span: 6,
						labelWidth: 120,
					},
					{
						label: '证书注册日期',
						prop: 'timeArea',
						type: 'daterange',
						format: 'yyyy-MM-dd',
						valueFormat: 'yyyy-MM-dd',
						span: 8,
						labelWidth: 120,
					},
				],
			};
		},
		...mapGetters(['permission']),
		permissionList() {
			return {};
		},
		ids() {
			let ids = [];
			this.selectionList.forEach(ele => {
				ids.push(ele.id);
			});
			return ids.join(',');
		},
	},
	created() {},
	methods: {
		activated() {
			this.refreshChange();
		},
		handlerevork(row) {
			this.dialogrevoke = true;
			this.revokeId = row.id;
		},
		//确认撤销
		handlerevoke() {
			if (!this.textarearevoke) {
				return this.$message({
					message: '请先输入撤销说明',
					type: 'warning',
				});
			}
			acserevoke({ id: this.revokeId, revokeDesc: this.textarearevoke }).then(res => {
				this.dialogrevoke = false;
				this.$message({
					type: 'success',
					message: '操作成功!',
				});
				this.onLoad(this.page);
			});
		},
		openpassword(row) {
			this.passwordis = row.id;
			this.dialogpassword = true;
		},
		toshenpi(row, types) {
			console.log(row);
			this.$router.push(`/zasgover/zjsh?id=${row}&type=${types}`);
		},
		//重置密码
		changepass() {
			this.$refs.numberValidateForm.validate(valid => {
				if (valid) {
					let item = String(this.numberValidateForm.password);
					updatePasswordNoOldPassword({
						password: md5(item),
						userId: this.passwordis,
					}).then(res => {
						this.$message({
							type: 'success',
							message: '操作成功!',
						});
						this.dialogpassword = false;
						this.numberValidateForm = {
							password: '',
							password1: '',
						};
					});
				} else {
					return false;
				}
			});
		},
		rowSave(row, done, loading) {
			add(row).then(
				() => {
					this.onLoad(this.page);
					this.$message({
						type: 'success',
						message: '操作成功!',
					});
					done();
				},
				error => {
					window.console.log(error);
					loading();
				}
			);
		},
		rowUpdate(row, index, done, loading) {
			update(row).then(
				() => {
					this.onLoad(this.page);
					this.$message({
						type: 'success',
						message: '操作成功!',
					});
					done();
				},
				error => {
					window.console.log(error);
					loading();
				}
			);
		},
		rowDel(row) {
			this.$confirm('确定将选择数据删除?', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					return remove(row.id);
				})
				.then(() => {
					this.onLoad(this.page);
					this.$message({
						type: 'success',
						message: '操作成功!',
					});
				});
		},
		searchReset() {
			this.query = {};
			this.onLoad(this.page);
		},
		searchChange(params, done) {
			this.query = params;
			this.page.currentPage = 1;
			this.onLoad(this.page, params);
			done();
		},
		selectionChange(list) {
			this.selectionList = list;
		},
		selectionClear() {
			this.selectionList = [];
			this.$refs.crud.toggleSelection();
		},
		handleDelete() {
			if (this.selectionList.length === 0) {
				this.$message.warning('请选择至少一条数据');
				return;
			}
			this.$confirm('确定将选择数据删除?', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					return remove(this.ids);
				})
				.then(() => {
					this.onLoad(this.page);
					this.$message({
						type: 'success',
						message: '操作成功!',
					});
					this.$refs.crud.toggleSelection();
				});
		},
		currentChange(currentPage) {
			this.page.currentPage = currentPage;
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize;
		},
		refreshChange() {
			this.onLoad(this.page, this.query);
		},
		handleSubmit(form, done, loading) {
			this.onLoad(this.page, form);
			done();
		},
		handleReset() {
			this.onLoad(this.page);
		},
		onLoad(page, params = {}) {
			this.loading = true;
			if (params.timeArea && params.timeArea.length) {
				params['confirmTimeStart'] = params.timeArea[0];
				params['confirmTimeEnd'] = params.timeArea[1];
			} else {
				params['confirmTimeStart'] = '';
				params['confirmTimeEnd'] = '';
			}
			delete params.timeArea;
			acseRegisterpage(page.currentPage, page.pageSize, params).then(res => {
				const data = res.data.data;
				this.page.total = data.total;
				this.data = data.records;
				this.loading = false;
				this.selectionClear();
			});
		},
	},
};
</script>

<style>
</style>
